import nt from './network'

export default {
    getMethods(opt) {
        return {
            //重置表单
            _resetForm(formName) {
                this.$refs[formName] && this.$refs[formName].resetFields();
            },
            //过滤json串中的空值
            _getNotNull(obj) {
                var ret = JSON.parse(JSON.stringify(obj));
                var scanList = [ret];
                while (scanList.length > 0) {
                    var obj = scanList[0];
                    scanList = scanList.slice(1)
                    for (var k in obj) {
                        let v = obj[k];
                        if (v === "" || 　v === undefined || v === null || 　v === NaN) {
                            delete obj[k];
                            continue;
                        }
                        var typename = typeof(obj[k]);
                        if (typename == 'object') {
                            if (obj[k] == null) {
                                delete obj[k];
                            } else {
                                scanList.push(obj[k]);
                            }
                        }
                    }
                }
                return ret;
            },
            /**
             * 删除、批量删除的确认弹出框
             * opt  参数对象
             * isBatchDel  是否是批量删除
             * */
            _actionConfirm(opt,isBatchDel) {
                let action = opt.action; //动作名称
                let objName = opt.objName; //操作对象名称
                let dataName = opt.dataName; //操作对象具体名称
                let onSuccess = opt.onSuccess;
                let tip = opt.tip;
                if(isBatchDel){
                    let data = opt.data; //操作对象
                    if (!data || data.length == 0) {
                        this.$message({
                            type: "info",
                            message: "请选选择需要" + action + '的' + objName
                        });
                        return;
                    }
                }

                if (!tip) { tip = "确定" + action + objName + ":" + dataName + " ？"; }

                this.$confirm(tip, "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                    .then(() => { try { onSuccess(); } catch (e) { console.log(e); } })
                    .catch(() => {
                        /*this.$message({
                            type: "info",
                            message: "已取消" + action
                        });*/
                    });

            },
            //获取当前系统时间
            _getDateNow() {
                var time = new Date(); // 程序计时的月从0开始取值后+1
                var m = time.getMonth() + 1;
                var date = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
                var hours = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
                var minute = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
                var seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
                m = m < 10 ? '0' + m : m;
                var prettyTime = time.getFullYear() + "-" + m + "-" +
                    date + " " + hours + ":" +
                    minute + ":" + seconds;
                return prettyTime;
            },

            /***
             * 初始化服务类别的级联选择
             * @param categoryId    系统条目值，例如 91
             * @param svrCategorys   初始的系统条目选项
             * @returns {Array}  级联选择所需的系统条目值格式，例如【92,91】
             */
            _initSysEntryCascader(categoryId, svrCategorys) {
                let path = [];
                let itemSelected = {};

                function getSysEntryPath(categoryList, categoryId) {
                    for (let item of categoryList) {
                        if (item.sysCategoryId == categoryId) {
                            path.unshift(item.sysCategoryId);
                            itemSelected = item;
                            return;
                        } else if (item.children && item.children.length > 0) {
                            getSysEntryPath(item.children, categoryId, path);
                        }
                    }
                }
                getSysEntryPath(svrCategorys, categoryId);
                while ((!$.isEmptyObject(itemSelected)) && itemSelected.sysCategoryPid != -1) {
                    getSysEntryPath(svrCategorys, itemSelected.sysCategoryPid);
                }
                return path;
            },
            notify(msg) {
                const h = this.$createElement;
                this.$message({
                    message: h('p', null, [
                        h('span', null, '连接失败: '),
                        h('i', { style: 'color: teal' }, msg)
                    ])
                });
            },
            /**
             * 将普通数据转化为分页数据
             * @param pagination  分页对象
             * @param data   普通数据
             * @returns {Array}  分页后的数据
             */
            _changeToPageData(pagination, data) {
                //debugger;
                let newData = [];
                let pageSize = pagination.pageSize;
                let currentPage = pagination.currentPage;
                let totalRecord = data.length;

                let bIndex = (currentPage - 1) * pageSize;
                let eIndex = currentPage * pageSize - 1;

                newData = data.filter((val, idx) => {
                    return idx >= bIndex && idx <= eIndex;
                });
                this.$set(pagination, 'totalRecord', totalRecord);
                return newData;
            },
            exportFile(url) {
                let link = document.createElement('a');
                link.style.display = 'none'
                link.href = url;

                document.body.appendChild(link);
                link.click();
            }
        }
    }
}